package com.talang.surfing.batch.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.Executor;

/**
 * @Description:
 * @author: wangwanbao
 * @Date: 2023/6/19 2:39 PM
 */
@Component
@Configuration
@EnableAsync(proxyTargetClass = true)
@Slf4j
public class AsyncConfig implements AsyncConfigurer {

    @Resource
    private SurfingProperties surfingProperties;

    @Bean()
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(surfingProperties.getCorePoolSize());
        executor.setMaxPoolSize(surfingProperties.getMaxPoolSize());
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("SurfingThread-");
        executor.initialize();
        executor.setDaemon(true);
        return executor;
    }

}
